package com.avic.system.service.impl;

import com.avic.system.dal.model.vo.MenuPermission;
import com.avic.system.dal.model.SystemPermission;
import com.avic.system.service.SystemPermissionService;
import com.avic.system.common.util.AssertUtil;
import com.avic.system.common.util.ex.BizErrorCode;
import com.avic.system.common.util.ex.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 系统权限serviceImpl
 *
 * @author: huangsheng
 * @date: 2017/10/24
 */
@Service("systemPermissionService")
public class SystemPermissionServiceImpl extends SystemAbstractServiceImpl implements SystemPermissionService {

    /**
     * 获取菜单权限数据异常
     *
     * @param menuId 菜单ID
     * @return SystemPermission
     */
    @Override
    public List<SystemPermission> getSystemPermission(String menuId) {
        try {
            List<SystemPermission> systemPermissions = systemPermissionMapper.queryPermissionByMenuId(menuId);
            return systemPermissions;
        } catch (Exception e) {
            logger.error("获取菜单权限数据异常：{}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "获取菜单权限数据异常");
        }
    }

    /**
     * 获取所有菜单的权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午9:08
     */
    @Override
    public List<MenuPermission> getMenuPermissions() {
        try {
            List<MenuPermission> menuPermissions = menuPermissionMapper.queryAllMenuAndPermission();
            return menuPermissions;
        } catch (Exception e) {
            logger.error("获取所有菜单的权限数据异常：{}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "获取所有菜单的权限数据查询数据库异常");
        }
    }

    /**
     * 查询角色拥有的权限ID集合
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午9:38
     */
    @Override
    public List<String> getPermissionByRoleId(String roleId) {
        return rolePermissionMapper.queryPermissionByRoleId(roleId);
    }

    /**
     * 新增权限
     *
     * @author: huangsheng
     * @date: 2017/10/26 下午2:52
     */
    @Override
    public String createSystemPermission(SystemPermission systemPermission) {

        int permissionCodeCount = systemPermissionMapper.checkPermissionCode(systemPermission.getPermissionCode());
        logger.info("查询到 permissionCode = {}， 的数量在库里是 {}", systemPermission.getPermissionCode(), permissionCodeCount);
        AssertUtil.isTrue(permissionCodeCount == 0, BizErrorCode.PERMISSION_CODE_IS_EXIST);
        try {
            String uuid = UUID.randomUUID().toString();
            logger.info("准备创建权限，ID = {}，菜单ID = {}", uuid, systemPermission.getMenuId());
            systemPermission.setId(uuid);
            int successInsertCount = systemPermissionMapper.insertSystemPermission(systemPermission);
            logger.info("成功插入 {} 条数据", successInsertCount);
            return uuid;
        } catch (Exception e) {
            logger.error("创建权限时异常：{}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "新增权限执行数据库异常");
        }
    }

    /**
     * 更新权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/26 下午3:02
     */
    @Override
    public void updateSystemPermission(SystemPermission systemPermission) {
        SystemPermission querySystemPermission = systemPermissionMapper.queryPermissionById(systemPermission.getId());
        AssertUtil.isTrue(querySystemPermission != null, BizErrorCode.PERMISSION_IS_NOE_EXIST);
        logger.info("查询到权限数据： permissionCode = {}", systemPermission.getPermissionCode());

        if (StringUtils.isNotBlank(systemPermission.getPermissionCode())) {
            if (!StringUtils.equals(querySystemPermission.getPermissionCode(), systemPermission.getPermissionCode())) {
                int permissionCodeCount = systemPermissionMapper.checkPermissionCode(systemPermission.getPermissionCode());
                logger.info("查询到 permissionCode = {}， 的数量在库里是 {}", systemPermission.getPermissionCode(), permissionCodeCount);
                AssertUtil.isTrue(permissionCodeCount == 0, BizErrorCode.PERMISSION_CODE_IS_EXIST);
            }
        }
        try {
            logger.info("修改权限，ID = {}", systemPermission.getId());
            int successUpdateCount = systemPermissionMapper.updateSystemPermission(systemPermission);
            logger.info("成功执行 {} 条数据", successUpdateCount);
        } catch (Exception e) {
            logger.error("修改异常时异常：{}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "更新权限数据操作数据库异常");
        }
    }

    /**
     * 删除权限
     *
     * @param permissionId 权限ID
     */
    @Override
    public void deleteSystemPermission(String permissionId) {
        try {
            logger.info("逻辑删除 ID = {} 的权限数据", permissionId);
            int successDeleteCount = systemPermissionMapper.deleteSystemPermission(permissionId);
            logger.info("成功执行 {} 条数据", successDeleteCount);
        } catch (Exception e) {
            logger.error("删除权限时异常：{}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "删除权限数据操作数据库异常");
        }
    }

    /**
     * 获取所有权限code
     *
     * @return
     */
    @Override
    public List<String> getAllSystemPermission() {

        return systemPermissionMapper.queryAllPermissionCodes();
    }
}
